﻿using System.Collections.Generic;
using System.Diagnostics;
using Microsoft.TeamFoundation.VersionControl.Client;
using TfsGiggle.Core.Tfs.Implementation;

namespace TfsGiggle.Core
{
    class VersionControlServerImpl : IVersionControlServer
    {
        private VersionControlServer _versionControlServer;

        public VersionControlServerImpl(VersionControlServer versionControlServer)
        {
            _versionControlServer = versionControlServer;
        }

        public bool ServerItemExists(string path, ItemType itemType)
        {
            var exists = _versionControlServer.ServerItemExists(path, itemType);
            return exists;
        }

        public IItem GetItem(string path)
        {
            var timer = new Stopwatch();
            var item = new ItemImpl(this, _versionControlServer.GetItem(path));
            timer.Stop();
            ETW.TfsGiggleProvider.EventWriteTFSGetItemEvent(path, timer.ElapsedTicks);
            return item;
        }

        public IChangeset GetChangeset(int changesetId, bool includeChanges, bool includeDownloadInfo)
        {
            return new ChangesetImpl(this, _versionControlServer.GetChangeset(changesetId, includeChanges, includeChanges));
        }

        public IChangeset[] QueryHistory(string path, VersionSpec versionSpec, int deletionId, RecursionType recursion, string user, VersionSpec versionFrom, VersionSpec versionTo, int maxCount, bool includeChanges, bool slotMode)
        {
            List<ChangesetImpl> changesetList = new List<ChangesetImpl>();
            foreach (Changeset changeset in _versionControlServer.QueryHistory(path, versionSpec, deletionId, recursion, user, versionFrom, versionTo, maxCount, includeChanges, slotMode))
            {
                changesetList.Add(new ChangesetImpl(this, changeset));
            }
            return changesetList.ToArray();
        }
    }
}